home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / lang / HeliOS3.lha / helios_demo_disk3 / source / Demo5_MultiSptCollide.src < prev    next >
Encoding:
Text File  |  1995-11-11  |  27.1 KB  |  996 lines

  1.  
  2.   \ ***********************************************************
  3.   \
  4.   \              MULTI SPRITES + COLLISIONS DEMO
  5.   \
  6.   \ ***********************************************************
  7.   \
  8.   \ This code demonstrates how to create multiple sprites and
  9.   \ then do collision detection.
  10.   \
  11.   \ This code builds upon the following Demos:
  12.   \
  13.   \ Demo1_SinglePF.src
  14.   \ Demo2_SinglePFCopper.src
  15.   \ Demo3_SimpleSprite.src
  16.   \ Demo4_MultiSprites.src
  17.   \
  18.   \ This code is then expanded in the following Demos:
  19.   \
  20.   \ Demo6_SimpleAnim.src
  21.   \ Demo7_MultiAnim.src
  22.   \
  23.   \ ***********************************************************
  24.   \
  25.   \
  26.   \ Note that it is easy to change the number of "Alien" sprites.
  27.   \
  28.   \ All you have to do is change the value of the CONSTANT Alien#.
  29.   \
  30.   \ If you locate Alien#, you will see that it normally has a value
  31.   \ of 15.  Try changing this to another value.
  32.   \
  33.   \
  34.   \ ***********************************************************
  35.   \ Re-initialise HeliOS dictionary to standard CORE vocabulary
  36.   \ ***********************************************************
  37.  
  38.   \ This should always be used at the start of any program which
  39.   \ is to be repeatedly recompiled.
  40.  
  41.   FORGET **CORE**
  42.  
  43.   \ *************************
  44.   \ Load include symbol files
  45.   \ *************************
  46.   \
  47.   \ These "include files" are pre-compiled (for speed) versions of the
  48.   \ Amiga includes and the Helios system includes.
  49.   \
  50.   \ Uncomment the lines below for standalone compilation, but otherwise
  51.   \ it is better to set these include files from the Helios Forth menus
  52.  
  53.   AMIGAINCLUDE HeliOS:HeliOS_AmigaInclude
  54.   USERINCLUDE  HeliOS:HeliOS_UserInclude
  55.  
  56.   \ ****************************************
  57.   \ Create display imagery file name strings
  58.   \ ****************************************
  59.  
  60.   \ These files are ordinary IFF's (and may be "PowerPacked" if required)
  61.   \
  62.   \ The pictures here will be loaded into each of the display BitMaps.
  63.   \
  64.  
  65.   $CONSTANTL Slice1Pic $Helios:Source/Data/Pic1$
  66.  
  67.   \ **************************************
  68.   \ Create display configuration constants
  69.   \ **************************************
  70.   \
  71.   \ Collect all "display-specific" parameters here and generate "named"
  72.   \ constants which make references easier than using numeric values.
  73.   \
  74.   \ Collecting these together here makes it easier to adjust things at any
  75.   \ time without having to search source code to replace values individually.
  76.   \
  77.  
  78.  
  79.   256                      CONSTANT DisplayHeight      \ Full PAL display
  80.   320                      CONSTANT DisplayWidth       \ Lores display
  81.   44                       CONSTANT DisplayTopLine     \ Display start
  82.  
  83.   DisplayWidth             CONSTANT Slice1Width        \ Lores width
  84.   DisplayWidth 32 +        CONSTANT Slice1RasterWidth  \ Raster=SWidth+32
  85.   DisplayHeight            CONSTANT Slice1Height       \ Slice height
  86.   DisplayHeight 32 +       CONSTANT Slice1RasterHeight \ Slice Raster=DHgt+32
  87.   0                        CONSTANT Slice1Mode         \ Lores
  88.   3                        CONSTANT Slice1Planes       \ Slice bitplanes
  89.  
  90.   \ The calculation below takes the number of bitplanes and calculates
  91.   \ how many colours this represents.
  92.   \
  93.   \ One bitplane gives two colours.
  94.   \
  95.   \ Each additional bitplane multiplies the number of colours by two.
  96.   \
  97.   \ Performing a single LSL operation on any number multipies by 2, so we
  98.   \ have a quick method of multiplying by two for our colour calculation.
  99.   \
  100.   \ In this case, we need "2 to the power 3", which is 2*2*2=8.
  101.   \
  102.   \ e.g.                              2*2*2
  103.   \                                   ^ ^ ^
  104.   \                                   Total number of planes = 3
  105.   \
  106.   \
  107.   \ So, we take the first value two
  108.   \
  109.   \ e.g.                              2*2*2
  110.   \                                   ^
  111.   \                                  Initial start value of 2
  112.   \
  113.   \ and we now need to multiply it by two "the_number_of_planes minus_one"
  114.   \ more times.
  115.   \
  116.   \ e.g.                              2*2*2
  117.   \                                     ^ ^
  118.   \                                     Total planes minus one
  119.   \
  120.   \
  121.   \ So, Number of colours = 2 operated on by LSL NumberOfPlanes-1 times.
  122.   \
  123.  
  124.   2 Slice1Planes 1- LSL    CONSTANT Slice1Colours      \ A-slice colours
  125.  
  126.   \ ***********************
  127.   \ Error handling routines
  128.   \ ***********************
  129.  
  130.   \ This error handler allows all errors to be routed via a comprehensive
  131.   \ sequential closedown routine, which is associated with the HeliOS
  132.   \ system error handler word ERROR".
  133.   \
  134.   \ When ERROR" senses an error, it prints an associated error message
  135.   \ delimited by '"' characters, and then closes everything down using the
  136.   \ routine CLOSEDOWN below which you have supplied.
  137.   \
  138.   \ This simplifies errors checks to the use of a single word, ERROR", which
  139.   \ displays a text message and closes eveything down.
  140.   \
  141.  
  142.   0 VARIABLE (CLOSEDOWN)
  143.  
  144.   : ?CLOSEDOWNERROR
  145.  
  146.   IF
  147.     CR
  148.     CR
  149.     TYPE
  150.     CR
  151.     CR
  152.     ." Press <Space> to quit!"
  153.     CR
  154.     CR
  155.     WAITSPACE
  156.     (CLOSEDOWN) @EXECUTE
  157.     QUIT
  158.   ELSE
  159.     DDROP
  160.   THEN
  161.   ;
  162.  
  163.   LATESTCFA VARIABLE ERROR1
  164.  
  165.   \ ****************************************
  166.   \ Create display pointer storage variables
  167.   \ ****************************************
  168.  
  169.   \ Here we create a set of "pointers", initially set to a "null" value.
  170.   \
  171.   \ These "pointers" are set up as "long addresses" when various components
  172.   \ of the display system are allocated and initialised.
  173.   \
  174.   \ Note that initially these are all set to zero, and we clear them back
  175.   \ to zero when we de-allocate the associated resource.
  176.   \
  177.   \ These DPOINTERs are all initially set to "null" by using '0.'.
  178.   \
  179.   \ When we allocate memory or Amiga system resources in the program at
  180.   \ run-time, these pointers are updated to contain the 32-bit address
  181.   \ of the newly allocated resource.
  182.   \
  183.   \ Subsequently the symbolic DPOINTER name can be used in your code to
  184.   \ represent the associated address.
  185.  
  186.   0. DPOINTER Display1             \ Main Display structure pointer
  187.   0. DPOINTER Slice1               \ Slice 1 Slice structure pointer
  188.  
  189.   0. DPOINTER Slice1_ColorMap      \ Slice 1 ColourMap structure pointer
  190.  
  191.   0. DPOINTER Slice1_RasInfo       \ Slice 1 RasInfo structure pointer
  192.  
  193.   0. DPOINTER Slice1_BMap          \ Slice 1 BitMap structure pointer
  194.  
  195.   0. DPOINTER Slice1_SliceControl  \ Slice 1 SliceControl structure pointer
  196.  
  197.   \ *************
  198.   \ Colour tables
  199.   \ *************
  200.  
  201.   \ Each colour entry requies 2 bytes of storage space
  202.  
  203.   CREATEL Slice1_ColorTable        \ Create longword pointer to table
  204.   Slice1Colours 2* 0 ALLOTFILL     \ Allocate Slice1 colours * 2 bytes
  205.  
  206.  
  207.   \ *************************************
  208.   \ Copper strip for graduated background
  209.   \ *************************************
  210.  
  211.   0. DVARIABLE Copper              \ 32-bit CopperList pointer store
  212.   0. DVARIABLE CopperLength        \ 32-bit CopperList length store
  213.  
  214.   : AddCopper                      \ Add custom copper list to display
  215.  
  216.   Display1                         \ We are adding CopperList to Display1
  217.   Copper D@
  218.   ADDCOPPERSTRIP
  219.   SORTSTRIPTABLE
  220.   LINKSTRIPS
  221.   DDROP
  222.   ;
  223.  
  224.   : RemCopper                      \ Remove custom copper list from display
  225.  
  226.   Display1                         \ We are removing CopperList from Display1
  227.   Copper D@
  228.   DFLAG
  229.   IF
  230.     REMCOPPERSTRIP
  231.     LINKSTRIPS
  232.     DDROP
  233.   ELSE
  234.     DDDROP
  235.   THEN
  236.   ;
  237.  
  238.   : Create_Copper
  239.  
  240.   COPPERSTART
  241.   Copper D!
  242.   0 [ DECIMAL ]  45 [ HEX ] FFFE COPPERWAIT  DROP   0100 18E COPPERMOVE  DROP
  243.   0 [ DECIMAL ]  60 [ HEX ] FFFE COPPERWAIT  DROP   0200 18E COPPERMOVE  DROP
  244.   0 [ DECIMAL ]  75 [ HEX ] FFFE COPPERWAIT  DROP   0300 18E COPPERMOVE  DROP
  245.   0 [ DECIMAL ]  90 [ HEX ] FFFE COPPERWAIT  DROP   0400 18E COPPERMOVE  DROP
  246.   0 [ DECIMAL ] 105 [ HEX ] FFFE COPPERWAIT  DROP   0500 18E COPPERMOVE  DROP
  247.   0 [ DECIMAL ] 120 [ HEX ] FFFE COPPERWAIT  DROP   0600 18E COPPERMOVE  DROP
  248.   0 [ DECIMAL ] 135 [ HEX ] FFFE COPPERWAIT  DROP   0700 18E COPPERMOVE  DROP
  249.   0 [ DECIMAL ] 150 [ HEX ] FFFE COPPERWAIT  DROP   0800 18E COPPERMOVE  DROP
  250.   0 [ DECIMAL ] 165 [ HEX ] FFFE COPPERWAIT  DROP   0900 18E COPPERMOVE  DROP
  251.   0 [ DECIMAL ] 180 [ HEX ] FFFE COPPERWAIT  DROP   0A00 18E COPPERMOVE  DROP
  252.   0 [ DECIMAL ] 195 [ HEX ] FFFE COPPERWAIT  DROP   0B00 18E COPPERMOVE  DROP
  253.   0 [ DECIMAL ] 210 [ HEX ] FFFE COPPERWAIT  DROP   0C00 18E COPPERMOVE  DROP
  254.   0 [ DECIMAL ] 225 [ HEX ] FFFE COPPERWAIT  DROP   0D00 18E COPPERMOVE  DROP
  255.   0 [ DECIMAL ] 240 [ HEX ] FFFE COPPERWAIT  DROP   0E00 18E COPPERMOVE  DROP
  256.   0 [ DECIMAL ] 255 [ HEX ] FFFE COPPERWAIT  DROP   0F00 18E COPPERMOVE  DROP
  257.   [ DECIMAL ]
  258.   COPPEREND
  259.   CopperLength D!   Copper D!
  260.   ADDCOPPER
  261.   ;
  262.  
  263.   : Free_Copper                  \ Closes down and frees copperlist memory
  264.  
  265.   RemCopper
  266.   Copper D@ FREEMEMORY
  267.   ;
  268.  
  269.   \ ***********************************
  270.   \ Create Display and Slice structures
  271.   \ ***********************************
  272.  
  273.   \ This routine simply makes blank structures, which then need to be
  274.   \ initialised later (in the CREATE_DISPLAY routine).
  275.  
  276.   : CREATE_DSLICES
  277.  
  278.   DS_SIZEOF MAKESTRUCTURE Display1 MAKEPOINTER  \ Main "Display" structure
  279.  
  280.   SL_SIZEOF MAKESTRUCTURE Slice1   MAKEPOINTER  \ Display "Slice" structure
  281.   ;
  282.  
  283.   : FREE_DSLICES
  284.  
  285.   Slice1    DDUP FREEMEMORY   CLEARPOINTER
  286.   Display1  DDUP FREEMEMORY   CLEARPOINTER
  287.   ;
  288.  
  289.   \ ******************************
  290.   \ Create RasInfo structures etc.
  291.   \ ******************************
  292.  
  293.   : CREATE_RASINFO
  294.  
  295.   \ First allocate and initialise complete RasInfo structures.
  296.   \
  297.   \ This routine automatically allocates all BitMaps etc.
  298.   \
  299.  
  300.   Slice1RasterWidth Slice1RasterHeight Slice1Planes  OPENRASINFO
  301.   DFLAG0= ERROR" Fail: RasInfo1"
  302.   Slice1_RasInfo MAKEPOINTER
  303.  
  304.   \ Set invisible area "sprite margins" for slice RasInfo
  305.  
  306.   16              Slice1_RasInfo         ri_RxOffset    INDEX!L
  307.   16              Slice1_RasInfo         ri_RyOffset    INDEX!L
  308.  
  309.   \ Store BitMap pointer - often useful for later reference
  310.  
  311.   Slice1_RasInfo  ri_BitMap INDEXD@L Slice1_BMap MAKEPOINTER
  312.   ;
  313.  
  314.   : FREE_RASINFO
  315.  
  316.   Slice1_RasInfo   DDUP CLOSERASINFO   CLEARPOINTER
  317.   ;
  318.  
  319.   \ ********************************
  320.   \ Create Display/Slice Copperlists
  321.   \ ********************************
  322.  
  323.   \ This function builds the main display copperlist by:
  324.   \
  325.   \ 1. Initialising the Slice data structure
  326.   \ 2. Calling MAKECOPSTRIP for the slice, to build a copperlist
  327.   \ 3. Calling MAKEDISPLAY to build the master Display copperlist
  328.   \
  329.  
  330.   : CREATE_DISPLAY
  331.  
  332.   \ First initialise main display structures
  333.  
  334.   Slice1                           Display1  DS_Slice     INDEXD!L
  335.  
  336.   Slice1Width                      Slice1    SL_DWidth    INDEX!L
  337.   Slice1Height                     Slice1    SL_DHeight   INDEX!L
  338.   DisplayTopLine                   Slice1    SL_DyOffset  INDEX!L
  339.   Slice1_RasInfo                   Slice1    SL_RasInfo   INDEXD!L
  340.   Slice1_ColorMap                  Slice1    SL_ColorMap  INDEXD!L
  341.   Slice1Mode                       Slice1    SL_Modes     INDEX!L
  342.  
  343.   \ Generate copper list information for each of the display slices
  344.  
  345.   Slice1 MAKECOPSTRIP
  346.   D0= ERROR" Fail: Slice1CopStrip"
  347.  
  348.   \ Make display
  349.  
  350.   Display1 MAKEDISPLAY
  351.   D0= ERROR" Fail: Display1"
  352.   ;
  353.  
  354.   : FREE_DISPLAY
  355.  
  356.   Display1                 FREEDISPLAY
  357.   Slice1                   FREECOPSTRIP
  358.   ;
  359.  
  360.   \ ********************************************************
  361.   \ Create SliceControl structures for double buffered slice
  362.   \ ********************************************************
  363.  
  364.   \ SliceControl structures are used to control any slices which perform
  365.   \ mapping or scrolling functions, or which require double or triple
  366.   \ playfield buffering.
  367.   \
  368.   \ In this case we have one slice which does not scroll, is not mapped,
  369.   \ but IS double buffered.
  370.   \
  371.  
  372.   : CREATE_SLICECONTROL
  373.  
  374.   \ Make SliceControl for double buffered bitmap display
  375.  
  376.   Slice1  0 0 MAKESLICECONTROL
  377.   DFLAG0= ERROR" Fail: SliceControl1"
  378.   Slice1_SliceControl MAKEPOINTER
  379.  
  380.   \ Install slice controls into HeliOS display control system
  381.  
  382.   Slice1_SliceControl  INSTALLSLICECONTROL
  383.   ;
  384.  
  385.   : FREE_SLICECONTROL
  386.  
  387.   CLEARSLICECONTROLS
  388.   Slice1_SliceControl  CLOSESLICECONTROL
  389.   ;
  390.  
  391.   \ These routines load an IFF picture into supplied BitMap, and correctly
  392.   \ initialises the supplied ColorTable.
  393.   \
  394.   \ The ColorTable is then used to create an initialised ColorMap structure.
  395.   \
  396.  
  397.   : CREATE_IMAGERY
  398.  
  399.   Slice1_BMap
  400.   Slice1_ColorTable
  401.   Slice1Pic
  402.   10 2 DOSLIB                        \ Call to internal HeliOS library
  403.   10 <> ERROR" Fail: Slice1Pic"
  404.  
  405.   Slice1_ColorTable  Slice1Colours MAKECOLORMAP  \ Allocate ColourMap
  406.   DFLAG0= ERROR" Fail: Slice1ColorMap"
  407.   Slice1_ColorMap MAKEPOINTER
  408.   ;
  409.  
  410.   : FREE_IMAGERY
  411.  
  412.   Slice1_ColorMap  DDUP FREECOLORMAP  CLEARPOINTER
  413.   ;
  414.  
  415.   \ **************
  416.   \ Collision Demo
  417.   \ **************
  418.  
  419.   \ ************************
  420.   \ Collision Demo constants
  421.   \ ************************
  422.  
  423.   \ Set up a number of constants which determine various aspects of the
  424.   \ demo, e.g. How many Bullets required
  425.   \
  426.   \ Change these values as required
  427.   \
  428.  
  429.   15 CONSTANT Bullet#           \ Number of Bullets available
  430.   15 CONSTANT Alien#            \ Number of Aliens available
  431.   8  CONSTANT BulletSpeed       \ Speed of Bullet
  432.   6  CONSTANT FiringRate        \ Speed of gun reload
  433.   4  CONSTANT GunSpeed          \ Speed of Gun movement
  434.   8  CONSTANT AlienMaxXSpeed    \ Maximum speed of Alien movement
  435.   4  CONSTANT AlienMaxYSpeed    \ Maximum speed of Alien movement
  436.  
  437.   \ ****************************************************
  438.   \ Create collision demo pointers and storage variables
  439.   \ ****************************************************
  440.  
  441.   0. DPOINTER GunSpriteSet         \ Gun sprite image
  442.   0. DPOINTER BulletSpriteSet      \ Bullet sprite image
  443.   0. DPOINTER AlienSpriteSet       \ Alien sprite images
  444.  
  445.   CREATE AlienTable                \ Alien sprite storage table
  446.   Alien# 4* 0 ALLOTFILL            \ Space allocated = Number_of_Aliens*4
  447.                                    \ This table has to hold a number of
  448.                                    \ 32-bit (4-byte) numbers, because
  449.                                    \ each Alien sprite is a 32-bit pointer
  450.  
  451.   CREATE BulletTable               \ Bullet sprite pointer storage table.
  452.   Bullet# 4* 0 ALLOTFILL           \ Space allocated = Number_of_Bullets*4
  453.                                    \ This table has to hold a number of
  454.                                    \ 32-bit (4-byte) numbers, because
  455.                                    \ each Bullet sprite is a 32-bit pointer
  456.  
  457.   CREATE  AlienCollHandlerTable    \ Alien collision handler table
  458.   Alien# 4* 0 ALLOTFILL
  459.  
  460.   CREATE  BulletCollHandlerTable   \ Bullet collision handler table
  461.   Bullet# 4* 0 ALLOTFILL
  462.  
  463.   0 VARIABLE     FireTimer         \ Gun reload rate store
  464.  
  465.   \ *************************
  466.   \ Sprite collision routines
  467.   \ *************************
  468.  
  469.   \ Note that in this instance the routines below are the same, and
  470.   \ could have been replaced by a single routine........
  471.  
  472.   \ This routine is called when a Bullet collides.
  473.  
  474.   : BulletHit
  475.  
  476.   2 DDROPS                        \ Drop collisionmask and hit-object pointer
  477.  
  478.   DDUP SpriteCtrl_CollFlag D+ 0!L \ Disable further collisions
  479.   REMOVESPRITE                    \ Remove Bullet
  480.   ;
  481.  
  482.   \ This routine is called when the Alien collides.
  483.  
  484.   : AlienHit
  485.  
  486.   2 DDROPS                        \ Drop collisionmask and "hitting" object
  487.   DDUP SpriteCtrl_CollFlag D+ 0!L \ Disable further collisions
  488.   REMOVESPRITE                    \ Remove Alien
  489.   ;
  490.  
  491.   \ ***************************
  492.   \ Create HitMasks and MeMasks
  493.   \ ***************************
  494.  
  495.   \ Collisions can only occur between objects which have coincident bits
  496.   \ set in HitMask-MeMask or MeMask-HitMask pairs
  497.   \
  498.   \ Each bit of the HitMask/MeMask pair have a corresponding position in
  499.   \ a collision table which stores an array of collision routines.
  500.   \
  501.   \ In the event of a collision, the routine in an object's Collision Table
  502.   \ corresponding to the HitMask-MeMask bit coincidence will be called
  503.   \ with 3 parameters: the two colliding objects and the collision HitMask
  504.   \
  505.   \ The stack on a collision call looks like this:
  506.   \
  507.   \ CollisionHitMask(l) = Top 32-bit value on stack
  508.   \ CollidingObject(l)  = 2nd 32-bit value on stack
  509.   \ This Object(l)      = 3rd 32-bit value on stack
  510.   \
  511.   \ Here are two examples:
  512.   \
  513.   \   00000000000000000000000000000010.  = Object A HitMask
  514.   \   00000000000000000000000000000000.  = Object B HitMask
  515.   \   00000000000000000000000000000001.  = Object C HitMask
  516.   \
  517.   \   00000000000000000000000000000000.  = Object A MeMask
  518.   \   00000000000000000000000000000011.  = Object B MeMask
  519.   \   00000000000000000000000000000000.  = Object C MeMask
  520.   \
  521.   \ Would allow A and B to collide, B and C to collide, but not A and C.
  522.   \
  523.   \ B and C would run the routine at position 1 in the collision table
  524.   \ A and B would run the routine at position 2 in the collision table
  525.   \
  526.   \ -----------------------------------------------------------
  527.   \
  528.   \   00000000000000000000000000000011.  = Object A HitMask
  529.   \   00000000000000000000000000000001.  = Object B HitMask
  530.   \   00000000000000000000000000000001.  = Object C HitMask
  531.   \
  532.   \   00000000000000000000000000000000.  = Object A MeMask
  533.   \   00000000000000000000000000000000.  = Object B MeMask
  534.   \   00000000000000000000000000000010.  = Object C MeMask
  535.   \
  536.   \ Would allow just A and C to collide
  537.   \
  538.   \ A and C would run the routine at position 2 in the collision table
  539.   \
  540.  
  541.   BIN
  542.  
  543.   00000000000000000000000000000001.  DCONSTANT  BulletHitMask
  544.   00000000000000000000000000000000.  DCONSTANT  AlienHitMask
  545.  
  546.   00000000000000000000000000000000.  DCONSTANT  BulletMeMask
  547.   00000000000000000000000000000001.  DCONSTANT  AlienMeMask
  548.  
  549.   DECIMAL
  550.  
  551.  
  552.   CREATEL BulletCollTable
  553.   -1 ,                        \ -1 = The "1" means there is 1 entry in table
  554.                               \      The "-" signifies that it is a HeliOS
  555.                               \      16-bit CFA rather than machine code
  556.  
  557.   FIND BulletHit ,            \ Find BulletHit and store HeliOS word CFA
  558.  
  559.   CREATEL AlienCollTable
  560.   -1 ,                        \ -1 = The "1" means there is 1 entry in table
  561.                               \      The "-" signifies that it is a HeliOS
  562.                               \      16-bit CFA rather than machine code
  563.  
  564.   FIND AlienHit ,             \ Find AlienHit and store HeliOS word CFA
  565.  
  566.  
  567.   \ *************************
  568.   \ Set up collision handlers
  569.   \ *************************
  570.  
  571.   : CREATE_COLLISIONS
  572.  
  573.   Bullet# 0
  574.   DO
  575.     GETCOLLHANDLER
  576.     DFLAG0= ERROR" Fail: Bullet CollHandler"
  577.     DDUP BulletCollHandlerTable I 4* + D!
  578.     BulletTable I 4* + D@  SpriteCtrl_CollHandler  INDEXD!L
  579.   LOOP
  580.  
  581.   Alien# 0
  582.   DO
  583.     GETCOLLHANDLER
  584.     DFLAG0= ERROR" Fail: Alien CollHandler"
  585.     DDUP AlienCollHandlerTable I 4* + D!
  586.     AlienTable I 4* + D@  SpriteCtrl_CollHandler  INDEXD!L
  587.   LOOP
  588.   ;
  589.  
  590.   : FREE_COLLISIONS
  591.  
  592.   Alien# 0
  593.   DO
  594.     Alien# 1- I - 4*
  595.     AlienCollHandlerTable +
  596.     D@ FREECOLLHANDLER
  597.   LOOP
  598.  
  599.   Bullet# 0
  600.   DO
  601.     Bullet# 1- I - 4*
  602.     BulletCollHandlerTable +
  603.     D@ FREECOLLHANDLER
  604.   LOOP
  605.   ;
  606.  
  607.   \ *****************
  608.   \ Boundary routines
  609.   \ *****************
  610.  
  611.   \ Alien boundary sense routine
  612.  
  613.   : AlienBoundary
  614.  
  615.   DDUP SETSTRUCTURE5
  616.   SpriteCtrl_VisiHit INDEX@L                 \ Get boundary hit mask word
  617.   0 BTST                                     \ Did it hit left boundary
  618.   IF
  619.     SpriteCtrl_UserData1 STRUCTURE5 @L       \ Reverse X-motion
  620.     NEGATE
  621.     SpriteCtrl_UserData1 STRUCTURE5 !L
  622.   ELSE
  623.     1 BTST                                   \ Did it hit right boundary
  624.     IF
  625.       SpriteCtrl_UserData1 STRUCTURE5 @L     \ Reverse X-motion
  626.       NEGATE
  627.       SpriteCtrl_UserData1 STRUCTURE5 !L
  628.     THEN
  629.   THEN
  630.  
  631.   2 BTST                                     \ Did it hit upper boundary
  632.   IF
  633.     DROP
  634.     SpriteCtrl_UserData2 STRUCTURE5 @L       \ Reverse Y-motion
  635.     NEGATE
  636.     SpriteCtrl_UserData2 STRUCTURE5 !L
  637.   ELSE
  638.     3 BTST                                   \ Did it hit lower boundary
  639.     IF
  640.       DROP
  641.       SpriteCtrl_UserData2 STRUCTURE5 @L     \ Reverse Y-motion
  642.       NEGATE
  643.       SpriteCtrl_UserData2 STRUCTURE5 !L
  644.     ELSE
  645.       DROP
  646.     THEN
  647.   THEN
  648.   ;
  649.  
  650.   \ Remove Bullet - called when bullet hits boundary at edge of display
  651.  
  652.   : BulletRemove
  653.  
  654.   RemoveSprite
  655.   ;
  656.  
  657.   \ ****************************
  658.   \ User input response routines
  659.   \ ****************************
  660.  
  661.   : Fire
  662.  
  663.   RAWKEY @ 64 =
  664.   JOY1FIRE OR
  665.   IF
  666.     FireTimer @ 0<
  667.     IF
  668.       FiringRate FireTimer !
  669.       Bullet# 0
  670.       DO
  671.         BulletTable I 4* + D@ SETSTRUCTURE1
  672.         SpriteCtrl_Running STRUCTURE1 @L 0=
  673.         IF
  674.           1 SpriteCtrl_Running STRUCTURE1 !L
  675.           GunSpriteSet 8. D+ D@L SETSTRUCTURE2
  676.  
  677.           SpriteCtrl_XPos STRUCTURE2 @L 5 +
  678.           SpriteCtrl_XPos STRUCTURE1 !L
  679.  
  680.           SpriteCtrl_YPos STRUCTURE2 @L 2 -
  681.           SpriteCtrl_YPos STRUCTURE1 !L
  682.  
  683.           SpriteCtrl_UserData1 STRUCTURE1
  684.           SpriteCtrl_YAdd STRUCTURE1 D!L
  685.  
  686.           BulletSpeed NEGATE SpriteCtrl_UserData1 STRUCTURE1 !L
  687.  
  688.           1 SpriteCtrl_CollFlag STRUCTURE1 !L
  689.           1 SpriteCtrl_Flags STRUCTURE1 !L
  690.  
  691.           0. STRUCTURE1 INSTALLSPRITE
  692.           LEAVE
  693.         THEN
  694.       LOOP
  695.     ELSE
  696.       FireTimer DEC
  697.     THEN
  698.   THEN
  699.   ;
  700.  
  701.   : MoveGun
  702.  
  703.   RAWKEY @ 78 =
  704.   JOY1LEFTRIGHT 0>
  705.   OR
  706.   IF
  707.      GunSpeed
  708.      19 320
  709.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  710.      LIMIT+!L
  711.   ELSE
  712.     RAWKEY @ 79 =
  713.     JOY1LEFTRIGHT 0<
  714.     OR
  715.     IF
  716.      GunSpeed NEGATE
  717.      19 320
  718.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  719.      LIMIT+!L
  720.     THEN
  721.   THEN
  722.   ;
  723.  
  724.   \ *********************
  725.   \ Create sprite objects
  726.   \ *********************
  727.  
  728.   : CREATE_SPRITES
  729.  
  730.   \ ----------
  731.   \ Gun sprite
  732.   \ ----------
  733.  
  734.   Slice1_BMap
  735.   154 272
  736.   13 15
  737.   1
  738.   MAKESPRITESET
  739.   DFLAG0= ERROR" Fail: Gun SpriteSet"
  740.   GunSpriteSet MAKEPOINTER
  741.  
  742.   GunSpriteSet 8. D+ D@L
  743.   DDUP  SpriteCtrl_XPos D+       170 -ROT !L
  744.         SpriteCtrl_YPos D+       239 -ROT !L
  745.  
  746.   Slice1_SliceControl   GunSpriteSet    INITSPRITESET
  747.  
  748.   \ --------------
  749.   \ Bullet sprites
  750.   \ --------------
  751.  
  752.   Slice1_BMap
  753.   159 279
  754.   3 7
  755.   1
  756.   MAKESPRITESET
  757.   DFLAG0= ERROR" Fail: Bullet SpriteSet"
  758.   BulletSpriteSet MAKEPOINTER
  759.  
  760.   Slice1_SliceControl   BulletSpriteSet    INITSPRITESET
  761.  
  762.   Bullet# 0
  763.   DO
  764.     BulletSpriteSet 8. D+ D@L CLONESPRITE
  765.     DFLAG0= ERROR" Fail: Bullet Sprite"
  766.     DDUP BulletTable I 4* + D!
  767.     SETSTRUCTURE1
  768.  
  769.     BulletCollTable        SpriteCtrl_CollTable      STRUCTURE1 D!L
  770.     BulletHitMask          SpriteCtrl_HitMask        STRUCTURE1 D!L
  771.     BulletMeMask           SpriteCtrl_MeMask         STRUCTURE1 D!L
  772.     -2                     SpriteCtrl_VisiZone       STRUCTURE1 !L
  773.     4                      SpriteCtrl_LeftZone       STRUCTURE1 !L
  774.     4                      SpriteCtrl_RightZone      STRUCTURE1 !L
  775.     4                      SpriteCtrl_UpZone         STRUCTURE1 !L
  776.     10                     SpriteCtrl_DownZone       STRUCTURE1 !L
  777.     ' BulletRemove CFA     SpriteCtrl_VisiForth      STRUCTURE1 !L
  778.   LOOP
  779.  
  780.   \ -------------
  781.   \ Alien sprites
  782.   \ -------------
  783.  
  784.   Slice1_BMap
  785.   24 276
  786.   14 9
  787.   1
  788.   MAKESPRITESET
  789.   DFLAG0= ERROR" Fail: Alien SpriteSet"
  790.   AlienSpriteSet MAKEPOINTER
  791.  
  792.   Slice1_SliceControl   AlienSpriteSet    INITSPRITESET
  793.  
  794.   Alien# 0
  795.   DO
  796.     AlienSpriteSet 8. D+ D@L CLONESPRITE
  797.     DFLAG0= ERROR" Fail: Alien Sprite"
  798.     DDUP AlienTable I 4* + D!
  799.     SETSTRUCTURE1
  800.  
  801.     ALienCollTable         SpriteCtrl_CollTable      STRUCTURE1 D!L
  802.     AlienHitMask           SpriteCtrl_HitMask        STRUCTURE1 D!L
  803.     AlienMeMask            SpriteCtrl_MeMask         STRUCTURE1 D!L
  804.     1                      SpriteCtrl_VisiZone       STRUCTURE1 !L
  805.     4                      SpriteCtrl_LeftZone       STRUCTURE1 !L
  806.     4                      SpriteCtrl_RightZone      STRUCTURE1 !L
  807.     4                      SpriteCtrl_UpZone         STRUCTURE1 !L
  808.     25                     SpriteCtrl_DownZone       STRUCTURE1 !L
  809.     ' AlienBoundary CFA    SpriteCtrl_VisiForth      STRUCTURE1 !L
  810.     SpriteCtrl_UserData1 STRUCTURE1 SpriteCtrl_XAdd  STRUCTURE1 D!L
  811.     SpriteCtrl_UserData2 STRUCTURE1 SpriteCtrl_YAdd  STRUCTURE1 D!L
  812.   LOOP
  813.   ;
  814.  
  815.   : FREE_SPRITES
  816.  
  817.   Alien# 0
  818.   DO
  819.     Alien# 1- I - 4* AlienTable +
  820.     DUP
  821.     D@ FREEMEMORY
  822.     D0!
  823.   LOOP
  824.  
  825.   AlienSpriteSet  DDUP FREESPRITESET CLEARPOINTER
  826.  
  827.   Bullet# 0
  828.   DO
  829.     Bullet# 1- I - 4* BulletTable +
  830.     DUP
  831.     D@ FREESPRITE
  832.     D0!
  833.   LOOP
  834.  
  835.   BulletSpriteSet DDUP FREESPRITESET CLEARPOINTER
  836.   GunSpriteSet    DDUP FREESPRITESET CLEARPOINTER
  837.   ;
  838.  
  839.   \ *****************
  840.   \ Re-Install Aliens
  841.   \ *****************
  842.  
  843.   : ?Install_Aliens
  844.  
  845.   \ Check to see if any Aliens are still running
  846.  
  847.   1                                             \ Put 1 flag on stack
  848.   Alien# 0                                      \ Loop for all Aliens
  849.   DO
  850.     AlienTable I 4* + D@ SETSTRUCTURE7
  851.     SpriteCtrl_Running STRUCTURE7 @L            \ If an Alien is running
  852.     IF
  853.       DROP 0                                    \ Change '1' on stack to '0'
  854.     THEN
  855.   LOOP
  856.  
  857.   \ If any Alien was running flag will now be '0' so next code is skipped
  858.  
  859.   IF
  860.     Alien# 0
  861.     DO
  862.       AlienTable I 4* + D@ SETSTRUCTURE7
  863.  
  864.       1              SpriteCtrl_CollFlag    STRUCTURE7 !L
  865.       1              SpriteCtrl_Flags       STRUCTURE7 !L
  866.       280 RND 26 +   SpriteCtrl_XPos        STRUCTURE7 !L
  867.       180 RND 26 +   SpriteCtrl_YPos        STRUCTURE7 !L
  868.       AlienMaxXSpeed RND 2 MAX 1 RND IF NEGATE THEN
  869.                      SpriteCtrl_UserData1   STRUCTURE7 !L
  870.       AlienMaxYSpeed RND 1 MAX 1 RND IF NEGATE THEN
  871.                      SpriteCtrl_UserData2   STRUCTURE7 !L
  872.       0. STRUCTURE7 INSTALLSPRITE
  873.     LOOP
  874.   THEN
  875.   ;
  876.  
  877.   \ *********************
  878.   \ Close down everything
  879.   \ *********************
  880.  
  881.   : CLOSEDOWN
  882.  
  883.   FREE_COLLISIONS
  884.   FREE_SPRITES
  885.   FREE_COPPER
  886.   FREE_SLICECONTROL
  887.   FREE_DISPLAY
  888.   FREE_IMAGERY
  889.   FREE_RASINFO
  890.   FREE_DSLICES
  891.   RESETERROR"
  892.   ;
  893.  
  894.   LATESTCFA (CLOSEDOWN) !
  895.  
  896.   : TestDisplay          \ Start of program
  897.  
  898.   SCRCLR
  899.   CR
  900.  
  901.   ."        **********************************************************"
  902.   CR 6 FPENSET
  903.   ."                     MULTI SPRITES + COLLISIONS DEMO"
  904.   CR 1 FPENSET
  905.   ."        **********************************************************"
  906.   CR
  907.   CR
  908.   ."        This code demonstrates how to create multiple sprites and "
  909.   CR
  910.   ."        then do collision detection."
  911.   CR
  912.   CR
  913.   ."        This code builds upon the following Demos:"
  914.   CR
  915.   CR
  916.   ."        Demo1_SinglePF.src"
  917.   CR
  918.   ."        Demo2_SinglePFCopper.src"
  919.   CR
  920.   ."        Demo3_SimpleSprite.src"
  921.   CR
  922.   ."        Demo4_MultiSprites.src"
  923.   CR
  924.   CR
  925.   ."        This code is then expanded in the following Demos:"
  926.   CR
  927.   CR
  928.   ."        Demo6_SimpleAnim.src"
  929.   CR
  930.   ."        Demo7_MultiAnim.src"
  931.   CR
  932.   ."        **********************************************************"
  933.   CR 6 FPENSET
  934.   ."                  Press <Space> or <L-Mouse> to see Demo"
  935.   CR 3 FPENSET
  936.   ."          Use a Joystick to move the gun turret and fire bullets"
  937.   CR
  938.   ."        **********************************************************"
  939.   CR
  940.  
  941.   WAITSPACE
  942.  
  943.   SCRCLR
  944.  
  945.   ERROR1 SETERROR"       \ Redirect system errors to our routine ERROR1
  946.  
  947.   CREATE_DSLICES
  948.   CREATE_RASINFO
  949.   CREATE_IMAGERY
  950.   CREATE_DISPLAY
  951.   CREATE_SLICECONTROL
  952.   CREATE_COPPER
  953.   CREATE_SPRITES
  954.   CREATE_COLLISIONS
  955.  
  956.   HeliOS_On
  957.  
  958.   GunSpriteSet 8. D+ D@L INSTALLSPRITE
  959.  
  960.   1 FrameRate !L
  961.  
  962.   Display1 SHOWDISPLAY
  963.  
  964.   BEGIN
  965.     WAITFRAME
  966.  
  967.     ?Install_Aliens
  968.  
  969.     Fire
  970.  
  971.     MoveGun
  972.  
  973.     ?TERMINAL 27 =
  974.   UNTIL
  975.  
  976.   -3 GunSpriteSet 8. D+ D@L SpriteCtrl_Flags INDEX!L
  977.  
  978.   Alien# 0
  979.   DO
  980.   -3 AlienTable I 4* + D@ SpriteCtrl_Flags INDEX!L
  981.   LOOP
  982.  
  983.   Bullet# 0
  984.   DO
  985.     -3 BulletTable I 4* + D@ SpriteCtrl_Flags INDEX!L
  986.   LOOP
  987.  
  988.   5 DELAY
  989.  
  990.   HeliOS_Off
  991.  
  992.   CLOSEDOWN
  993.   ;
  994.  
  995.   TestDisplay
  996.